package com.shuyihui.rxhttp.func

import android.util.Log
import com.google.gson.*
import org.json.JSONObject
import java.lang.reflect.Type

/**
 * 描述：处理String 类型后台返回null
 */
class StringConverter : JsonSerializer<String?>, JsonDeserializer<String> {
    override fun serialize(
        src: String?, typeOfSrc: Type,
        context: JsonSerializationContext
    ): JsonElement {
        Log.e(TAG, "serialize: $src\n")
        if (typeOfSrc is JSONObject) {
            Log.e(TAG, "serialize: " + "ssssssssssssssssssssssssssssss")
        }
        return if (src == null) {
            JsonPrimitive("")
        } else {
            JsonPrimitive(src.toString())
        }
    }

    @Throws(JsonParseException::class)
    override fun deserialize(
        json: JsonElement, typeOfT: Type,
        context: JsonDeserializationContext
    ): String {
        if (json.isJsonObject) {
        }
        return json.asJsonPrimitive.asString
    }

    companion object {
        private const val TAG = "StringConverter"
    }
}